<?php

namespace addon\lian\shop\controller;

use addon\lian\dict\LianDict;
use addon\lian\model\Config as ConfigModel;
use addon\lian\model\LianLevel as LianLevelModel;
use addon\lian\model\LianWithdraw;
use app\model\goods\Goods as GoodsModel;
use app\model\system\Document;
use app\shop\controller\BaseShop;

/**
 *  分销设置
 */
class Config extends BaseShop
{

    /**
     *  分销基础设置
     */
    public function basics()
    {
        $model = new ConfigModel();

        if (request()->isJson()) {
            $data = [
                'is_open' => input('is_open', 0), //开关状态，0=关闭，1=开启
                'up_register' => input('up_register', 0), //注册升级：0否，1是
                'up_way' => input('up_way', LianDict::ORDERPAY), //升级触发方式，0=支付后，1=完成后
                'take_type' => input('take_type', LianDict::MONEY), //提成类型：0余额，1佣金，2积分，3数字积分
                'settle_way' => input('settle_way', LianDict::ORDERPAY), //结算方式：0订单支付后，1订单完成后
                'settle_cycle' => input('settle_cycle', 0), //结算周期：按天
                'count_type' => input('count_type', LianDict::ORDERMONEY), //计算方式：0支付金额，1商品现价，2商品原价
                'first_reward_frozen' => input('first_reward_frozen', 0), //直推奖励冻结：0否，1是
                'is_publine' => input('is_publine', 0),  //是否开启公排
                'publine_mode' => input('publine_mode', 0),  //公排模式
                'publine_type' => input('publine_type', 1),  //公排类型
                'inline_way' => input('inline_way', 1),  //入排模式
                'is_store' => input('is_store', 0),  //是否匹配多门店订单
                'is_fusee' => input('is_fusee', 0),  //是否开启复购见单奖
                'order_num' => input('order_num', 0),  //复购见单规则,order_num下几单
                'level_num' => input('level_num', 0),  //复购见单规则,level开启几层
                'fugou_num' => input('fugou_num', 0),  //复购见单规则,fugou_num复购的第几单
                'fusee_level' => input('fusee_level', 0),  //复购见点层级限制
                'create_time' => time(),
                'update_time' => time(),
            ];

            $res = $model->setLianBasicsConfig($data, 1, $this->site_id);
            return $res;
        } else {
            $basics = $model->getLianBasicsConfig($this->site_id);
            $this->assign('basics_info', $basics[ 'data' ][ 'value' ]);

            $lian = $model->getLianConfig($this->site_id)[ 'data' ][ 'value' ];

            return $this->fetch('config/basics');
        }

    }

    /**
     * 分销协议设置
     */
    public function agreement()
    {
        $model = new ConfigModel();

        if (request()->isJson()) {
            $data = [
                'is_agreement' => input('is_agreement', ''),//是否显示申请协议
                'agreement_title' => input('agreement_title', ''),//协议标题
                'agreement_content' => input('agreement_content', ''),//协议内容
                'img' => input('img', ''),//申请页面顶部图片
            ];
            $res = $model->setFenxiaoAgreementConfig($data, 1, $this->site_id);
            return $res;
        } else {
            $agreement = $model->getFenxiaoAgreementConfig($this->site_id);
            $this->assign('agreement_info', $agreement[ 'data' ][ 'value' ]);

            $document_model = new Document();
            $document = $document_model->getDocument([ [ 'site_id', '=', $this->site_id ], [ 'app_module', '=', 'shop' ], [ 'document_key', '=', 'FENXIAO_AGREEMENT'] ]);
            $this->assign('document', $document[ 'data' ]);

            return $this->fetch('config/agreement');
        }
    }

    /**
     *  分销结算设置
     */
    public function settlement()
    {
        $model = new ConfigModel();
        if (request()->isJson()) {
            $transfer_type = '';
            if (!empty(input('transfer_type'))) {
                $transfer_type = implode(',', input('transfer_type'));
            }
            $data = [
                'account_type' => input('account_type', ''),//佣金计算方式
                'settlement_day' => input('settlement_day', ''),//天数
                'withdraw' => input('withdraw', ''),//最低提现额度
                'withdraw_rate' => input('withdraw_rate', ''),//佣金提现手续费
//                'min_no_fee' => input('min_no_fee', ''),//最低免手续费区间
//                'max_no_fee' => input('max_no_fee', ''),//最高免手续费区间
                'withdraw_status' => input('withdraw_status', ''),//提现审核
                'withdraw_type' => input('withdraw_type', ''),//提现方式,

                'transfer_type' => $transfer_type,//转账方式,
                'is_auto_transfer' => input('is_auto_transfer', 0),//是否自动转账 1 手动转账  2 自动转账
//                'min' => input('min', 0),//提现最低额度
                'max' => input('max', 0),//提现最高额度
            ];
            $res = $model->setFenxiaoSettlementConfig($data, 1, $this->site_id);
            return $res;
        } else {
//            $settlement = $model->getFenxiaoSettlementConfig($this->site_id);
//            $this->assign('settlement_info', $settlement[ 'data' ][ 'value' ]);
            $withdraw_config = $model->getFenxiaoWithdrawConfig($this->site_id)[ 'data' ][ 'value' ] ?? [];
            $this->assign('withdraw_info', $withdraw_config);

            $fenxiao_withdraw_model = new FenxiaoWithdraw();
            $transfer_type_list = $fenxiao_withdraw_model->getTransferType($this->site_id);
            $transfer_type_list[ 'balance' ] = '余额';
            $this->assign('transfer_type_list', $transfer_type_list);

            return $this->fetch('config/settlement');
        }

    }

    /**
     *  分销文字设置
     */
    public function words()
    {
        $model = new ConfigModel();
        if (request()->isJson()) {
            $data = [
                'concept' => input('concept', ''),//分销概念
                'fenxiao_name' => input('fenxiao_name', ''),//分销商名称
                'withdraw' => input('withdraw', ''),//提现名称
                'account' => input('account', ''),//佣金
                'my_team' => input('my_team', ''),//我的团队
                'child' => input('child', ''),//下线
            ];

            $res = $model->setFenxiaoWordsConfig($data, 1, $this->site_id);
            return $res;
        } else {
            $config_info = $model->getFenxiaoWordsConfig($this->site_id)[ 'data' ][ 'value' ];
            $this->assign('config_info', $config_info);

            return $this->fetch('config/words');
        }

    }

    /**
     * 活动规则
     */
    public function promoteRule()
    {
        $document_model = new Document();
        if (request()->isJson()) {
            $content = input('content', '');
            $res = $document_model->setDocument('分销推广规则', $content, [ [ 'site_id', '=', $this->site_id ], [ 'app_module', '=', 'shop' ], [ 'document_key', '=', 'FENXIAO_PROMOTE_RULE'] ]);
            return $res;
        } else {
            $document = $document_model->getDocument([ [ 'site_id', '=', $this->site_id ], [ 'app_module', '=', 'shop' ], [ 'document_key', '=', 'FENXIAO_PROMOTE_RULE'] ]);
            $this->assign('document', $document[ 'data' ]);

            return $this->fetch('config/promote_rule');
        }
    }
}